package org.example.JUC;

public class whileABC {
    /**
     * 循环打印abc
     */
    private static final int N = 100;//定义循环次数
    private static int i = 1;//共享变量控制
    private static final Object lock = new Object();//锁对象用于同步线程

    public static void main(String[] args) {
        Thread th1 = new Thread(()->{printChar('A',0);});
        Thread th2 = new Thread(()->{printChar('B',1);});
        Thread th3 = new Thread(()->{printChar('C',2);});

        th1.start();
        th2.start();
        th3.start();
    }
    private static void printChar(char c,int target){
        try {
            while (i<=N){
                synchronized (lock){
                    while (i%3!=target){
                        if(i>N){
                            break;
                        }
                        lock.wait();
                    }
                    if (i > N) break;
                    System.out.println(c);
                    i++;
                    lock.notifyAll();
                }
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

}
